package com.sidneysimmons.financially.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.sidneysimmons.financially.Category;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.activities.AccountActivity;

public class NewCategoryDialogFragment extends DialogFragment {
	
	/** Used by the FragmentManager to identify this fragment. */
	public static String TAG = "new_category_dialog";
	
	private View newCategoryView;
	
	public static NewCategoryDialogFragment newInstance() {
		NewCategoryDialogFragment fragment = new NewCategoryDialogFragment();
    	
    	/* // We could dynamically put data into the dialog here
        Bundle bundle = new Bundle();
        bundle.putInt("title", title);
        fragment.setArguments(bundle); */
    	
        return fragment;
    }

	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    	// Use this to get dynamically put data, everything is static now
        // int title = getArguments().getInt("title");
    	
    	// Inflate the view to use
    	Context context = getActivity();
    	LayoutInflater inflater = LayoutInflater.from(context);
    	newCategoryView = inflater.inflate(R.layout.new_category_dialog, null);
    	
        return new AlertDialog.Builder(context)
        		.setView(newCategoryView)
                .setPositiveButton(R.string.new_category_dialog_text_create,
                    new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// populate Category and send back to UI
							AccountActivity activity = (AccountActivity) getActivity();
							activity.validateAndCreateNewCategory(activity.getAccountInFocus(), populateCategory(newCategoryView));
						}
                    }
                )
                .setNegativeButton(R.string.new_category_dialog_text_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	// Let the system close the dialog
                        }
                    }
                )
                .create();
    }
	
	/**
     * Populates and returns a new Category object.  Populates from all the fields on the 
     * AlertDialog view.
     * 
     * @param newCategoryView - the View to get the data from
     * @return a populated Category object
     */
    protected Category populateCategory(View newCategoryView) {
    	Category category = new Category();
    	
    	// Get fields here
    	EditText categoryNameField = (EditText) newCategoryView.findViewById(R.id.new_category_dialog_edittext_category_name);
    	
    	// Set data here
    	category.setTitle(categoryNameField.getText().toString().trim());
    	
    	return category;
    }
}